package org.qf.web;


import cn.dsna.util.images.ValidateCode;
import com.github.pagehelper.PageInfo;
import org.qf.pojo.Role;
import org.qf.pojo.User;
import org.qf.service.RoleService;
import org.qf.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    @RequestMapping("/login")
    public String login(String username, String password,String imgCode, Model model, HttpSession session) {
        String code = (String)session.getAttribute("code");
        if(imgCode.equalsIgnoreCase(code)){
            User login = userService.login(username, password);
            if (login != null) {
                //登录成功
                //把用户名存储起来
                session.setAttribute("username", username);
                session.setAttribute("id", login.getId());
                return "home";
            } else {
                return "login";
            }
        }
        else
        {
            return "login";
        }
    }

    @RequestMapping("/Register")
    public String Register(User user, Model model) {
        int i=userService.register(user);
        if (i>0)
        {
            model.addAttribute("result","注册成功!");
            return "login";
        }
        else {
            model.addAttribute("result","注册失败!");
            return "register";
        }
    }

    @RequestMapping("/RegisterHtml")
    public String RegisterHtml(Model model) {
        return "register";
    }


    //查询
    @RequestMapping("/showUser")
    public String userList(Model model, @RequestParam(defaultValue = "1") int pageNum,String name) {
        //去数据库查询，把数据查询出来，再跳转页面
        System.out.println("======================");
        PageInfo<User> pageInfo = userService.showUser(pageNum,name);
        System.out.println(pageInfo.getList());
        System.out.println(pageInfo + "======================");
        //把集合数据存储起来
        model.addAttribute("pageInfo", pageInfo);
        return "userList";
    }

    @RequestMapping("/imgCode")
    public void imgCode(HttpServletResponse response,HttpSession session) {

        ValidateCode validateCode = new
                ValidateCode(120, 40, 4, 10);
        //生成验证码
        String code = validateCode.getCode();
        try {
            //输出
            validateCode.write(response.getOutputStream());
            session.setAttribute("code",validateCode.getCode());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/addUser")
    public String addUser(HttpServletResponse response,HttpSession session,Model model,User user) {
        int i= userService.addUser(user);
        List<Role> roles = roleService.showRole();
        model.addAttribute("roles",roles);
        if (i>0)
        {
            model.addAttribute("result","添加成功!");
            return "redirect:/showUser";
        }
        else {
            model.addAttribute("result","添加失败!");
            return "addUser";
        }
    }

    @RequestMapping("/EditUserHtml")
    public String EditUserHtml(HttpServletResponse response,String id,Model model,HttpSession session) {
        User user=userService.selectByID(id);
        List<Role> roles = roleService.showRole();
        model.addAttribute("roles",roles);
        model.addAttribute("user",user);
        return "userUpdate";
    }

    @RequestMapping("/EditUser")
    public String EditUser(HttpServletResponse response,User user,Model model) {
        int i=userService.EditUser(user);
        List<Role> roles = roleService.showRole();
        model.addAttribute("roles",roles);
        if(i>0){
            //增加成功
            return "redirect:/showUser";
        }else{
            return "userUpdate";
        }
    }
    //增加功能
    @RequestMapping("/addUserHtml")
    public String addUserHtml(Model model,HttpSession session){
        //查询所有的角色名称跳转到页面
        List<Role> roles = roleService.showRole();
        model.addAttribute("roles",roles);
        return "userAdd";
    }
    //删除
    @RequestMapping("/deleteUser")
    public String deleteUser(Model model,HttpSession session,User user){
        int i=userService.DeleteUser(user);

        if (i>0)
        {
            model.addAttribute("result","删除成功!");
        }
        else {
            model.addAttribute("result","删除失败!");
        }
        return "redirect:/showUser";
    }

    //删除
    @RequestMapping("/editPassword")
    public String editPassword(Model model,HttpSession session,User user){
        int i=userService.editPassword(user);
        if (i>0)
        {
            model.addAttribute("result","改密码成功!");
        }
        else {
            model.addAttribute("result","改密码失败!");
        }
        return "login";
    }
    @RequestMapping("/editPasswordHTML")
    public String editPasswordHTML(Model model,HttpSession session,User user){

        return "password";
    }

}
